package cn.dlc.com.account.service.impl;

import cn.dlc.com.account.dao.AccountDao;
import cn.dlc.com.account.service.AccountService;
import cn.dlc.com.log.dao.DuplicationDao;
import cn.dlc.com.log.dao.LocalConfirmLogDao;
import cn.dlc.com.log.entity.AccountChangeEvent;
import cn.dlc.com.log.entity.Duplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 邓立川
 * @date 2021/1/19 11:36
 * @desc 添加当前类的描述信息
 */
@Service
@Slf4j
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private DuplicationDao duplicationDao;

    @Override
    public void transfer(AccountChangeEvent accountChangeEvent) {

        // 幂等校验，判断有没有消费过消息，消费过不再消费
        if(duplicationDao.existsById(accountChangeEvent.getTxNo())) {
            return ;
        }

        // 没有执行过，增加余额
        accountDao.updateAccount(accountChangeEvent.getAccountNo(), accountChangeEvent.getAmount());

        // 添加日志
        duplicationDao.save(new Duplication(accountChangeEvent.getTxNo(),new Date()));

        /*if(accountChangeEvent.getAmount() == 40){
            throw new RuntimeException("手工制造异常");
        }*/
    }
}
